#pragma once

#include <iostream>

template<typename T>
class WVector
{
public:

    typedef T value;
    typedef T *iterator; // vector的迭代器就是个指针而已
    typedef T &reference;

    WVector()
    {
        _data = new T[_len];
        _start = _data;
    }

    virtual ~WVector()
    {
        delete [] _data;
        _data = nullptr;
    }

    void push_back(const T &data)
    {
        if (_pos < _len)
        {
            *(_start + _pos) = data;
            _pos++;
        }
        else
        {
            // 扩容
            // std::cout << "the wvector is full";
            addCapability();
            *(_start + _pos) = data;
            _pos++;
        }
    }

    void addCapability()
    {
        long long int newLen = _len * 3 / 2;
        T *newData = new T[newLen];
        for(long long int i = 0; i < _pos; ++i)
        {
            *(newData + i) = *(_data + i);
        }

        delete [] _data;
        _data = newData;
        _start = _data;
        _len = newLen;
    }

    T pop_back()
    {
        if (_pos > 0)
        {
            _pos--;
            return *(_start + _pos);
        }
        else
        {
            return T();
        }
    }

    long long int size()
    {
        return _pos;
    }

    iterator begin()
    {
        return _start;
    }

    iterator end()
    {
        return _start + _pos;
    }

    T& operator[](long long int n)
    {
        return *(_start + n);
    }

private:
    long long int _len = 10;
    long long int _pos = 0;
    T *_data = nullptr;
    iterator _start = nullptr;
};
